<?php
declare (strict_types = 1);

namespace app\controller\backend;

use app\model\AttachmentCate;
use app\validate\AttachmentCateValidate;
use think\exception\ValidateException;


class AttachmentCateController extends BaseController
{
    /**
     * 显示资源列表
     *
     * @return \think\response\Json
     * @throws \app\exception\ModelException
     */
    public function index(AttachmentCate $attachmentCate): \think\response\Json
    {
        $where = [
           ['seller_id' ,'=', $this->admin['seller_id']],
        ];
        $title = input('title');
        if(!empty($title)){
            array_push($where,['title' ,'like', '%'.$title.'%']);
        }
        $attachmentCateList = $attachmentCate->getAllCustomData($where)['data'];
        $attachmentCateList = makeTree($attachmentCateList);
        array_unshift($attachmentCateList,[
            'id' => 0,
            'parent_id' => 0,
            'path' => 0,
            'title'=> '全部分类',
            'children' => [],
        ]);
        return jsonReturn(0,'success',$attachmentCateList);
    }

    /**
     * 保存新建的资源
     *
     * @return \think\response\Json
     * @throws \app\exception\ModelException
     * @throws \app\exception\ModelNotUniqueException
     */
    public function save(AttachmentCate $attachmentCate): \think\response\Json
    {
        if(request()->isPost()){
            $param = input('post.');
            // 数据验证
            try{
                validate(AttachmentCateValidate::class)->scene('save')->check($param);
            }catch(ValidateException $e){
                return jsonReturn(-1, $e->getError());
            }
            $param['seller_id'] = $this->admin['seller_id'];
            $attachmentCate->saveUnique(['title'=>$param['title'],'seller_id'=>$param['seller_id'],'parent_id'=>$param['parent_id']],'栏目已经存在');
            // 完善栏目数据
            if ($param['parent_id']) {
                $parentCate = $attachmentCate->getCustomData(['id' => $param['parent_id'], 'seller_id' => $param['seller_id']])['data'];
                $param['path'] = $parentCate['path'] . '-' . $param['parent_id'];
            } else {
                $param['path'] = 0;
            }
            $res = $attachmentCate -> addAttachmentCate($param);
            return json($res);
        }
        return jsonReturn(-3,'请求方法错误');
    }

    /**
     * 显示指定的资源
     *
     * @return \think\response\Json
     * @throws \app\exception\ModelException
     * @throws \app\exception\ModelEmptyException
     */
    public function read(AttachmentCate $attachmentCate): \think\response\Json
    {

        $id = (int)input('id');
        if(!$id){
            return jsonReturn(-1,'分类ID不能为空');
        }
        $where = [
            'id' => $id,
        ];
        // 其他逻辑
        $res = $attachmentCate->getAttachmentCate($where);
        return json($res);

    }

    /**
     * 保存更新的资源
     * @return \think\response\Json
     * @throws \app\exception\ModelException
     */
    public function update(AttachmentCate $attachmentCate): \think\response\Json
    {
        if(request()->isPost()){
            $param = input('post.');
            try {
                validate(AttachmentCateValidate::class)->scene('update')->check($param);
            } catch (ValidateException $e) {
                return jsonReturn(-1, $e->getError());
            }
            $where = [
                'id' => $param['id'],
                'seller_id' => $this->admin['seller_id'],
            ];
            $res = $attachmentCate -> updateAttachmentCate($where,$param);
            return json($res);
        }
        return jsonReturn(-3,'请求方法错误');
    }

    /**
     * 删除指定资源
     *
     * @throws \app\exception\ModelException
     */
    public function delete(AttachmentCate $attachmentCate): \think\response\Json
    {
        if(request()->isPost()){
            $id = (int)input('id');
            if(!$id){
                return jsonReturn(-1,'分类ID不能为空');
            }
            $where = [
                'id' => $id,
                'seller_id' => $this->admin['seller_id']
            ];
            $subCate = $attachmentCate -> getAllCustomData(['seller_id'=>$this->admin['seller_id'],'parent_id'=>$id])['data'];
            if(!empty($subCate)){
                return jsonReturn(-1,'栏目下面有子栏目，不能直接删除');
            }
            $cate = $attachmentCate -> getCustomData($where,'attachment')['data']->toArray();
            if(!empty($cate['attachment'])){
                return jsonReturn(-2,'栏目下有附件不能直接删除');
            }
            $res = $attachmentCate->delAttachmentCate($where);
            return json($res);
        }
        return jsonReturn(-3,'请求方法错误');
    }
}
